﻿using System;
using System.Collections.Generic;
using System.Data;
using Loki.Data;

namespace Luna.Data.Budget
{
    /// <summary>
    /// Mapper for data interface ILineData.
    /// </summary>
    public partial class LineDataMapper : LunaMapper<LineData, ILineData>
    {
        public LineDataMapper()
        {
            GetAllRequest = "Budget.line_select_all";
            GetRequest = "Budget.line_select";
            InsertRequest = "Budget.line_insert";
            UpdateRequest = "Budget.line_update";
            DeleteRequest = "Budget.line_delete";
            GetAllByBudgetIDRequest = "Budget.line_select_by_budgetid";
        }

        private const string BudgetIDColumnName = "BudgetID";
        private const string CategoryIDColumnName = "CategoryID";
        private const string ParentIDColumnName = "ParentID";
        private const string CodeColumnName = "Code";
        private const string NameColumnName = "Name";
        private const string AmountColumnName = "Amount";
        private const string DescriptionColumnName = "Description";

        public string GetAllByBudgetIDRequest { get; set; }

        protected override void LoadCoreData(ILineData entity, IDataRecord record)
        {
            entity.BudgetID = record.GetGuid(BudgetIDColumnName);
            entity.CategoryID = record.GetNullableGuid(CategoryIDColumnName);
            entity.ParentID = record.GetNullableGuid(ParentIDColumnName);
            entity.Code = record.GetString(CodeColumnName);
            entity.Name = record.GetString(NameColumnName);
            entity.Amount = record.GetNullableDecimal(AmountColumnName);
            entity.Description = record.GetString(DescriptionColumnName);
        }

        protected override void MapSaveParameters(IDatabaseCommand command, ILineData entity)
        {
            command.AddParameter(BudgetIDColumnName, entity.BudgetID);
            command.AddParameter(CategoryIDColumnName, entity.CategoryID);
            command.AddParameter(ParentIDColumnName, entity.ParentID);
            command.AddParameter(CodeColumnName, entity.Code);
            command.AddParameter(NameColumnName, entity.Name);
            command.AddParameter(AmountColumnName, entity.Amount);
            command.AddParameter(DescriptionColumnName, entity.Description);
            command.AddParameter(IDParameterName, entity.PK);
        }

        public IEnumerable<ILineData> GetAllByBudgetID(IDbTransaction transaction, Guid budgetID)
        {
            return ExecuteGetAll(
                    transaction,
                    GetAllByBudgetIDRequest,
                    c =>
                    {
                        c.AddParameter(BudgetIDColumnName, budgetID);
                    });
        }
    }
}